package com.lilicould.service.impl;

import com.lilicould.dao.TenantDao;
import com.lilicould.service.TenantService;
import com.lilicould.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.SQLException;
import java.util.ArrayList;

public class TenantServiceImpl implements TenantService {
    @Override
    public ArrayList<String> selectTenantPhones(String tenantId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        TenantDao tenantDao = sqlSession.getMapper(TenantDao.class);
        ArrayList<String> tenantPhones = tenantDao.selectTenantPhones(tenantId);
        sqlSession.close();
        return tenantPhones;
    }

    @Override
    public boolean updateTenant(String tenantId, String name, String bankAccount, String emergencyName, String emergencyPhone) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        TenantDao tenantDao = sqlSession.getMapper(TenantDao.class);
        int result = tenantDao.updateTenant(tenantId, name, bankAccount, emergencyName, emergencyPhone);
        return result > 0;
    }

    @Override
    public boolean updateTenantPhones(String op, String tenantId, String phone ,String newPhone) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        TenantDao tenantDao = sqlSession.getMapper(TenantDao.class);
        int result = 0;
        try {
            switch (op) {
                case "delete":
                    result = tenantDao.deleteTenantPhone(tenantId, phone);
                    return result > 0;
                case "update":
                    result = tenantDao.updateTenantPhone(tenantId, phone, newPhone);
                    return result > 0;
                case "add":
                    result = tenantDao.insertTenantPhone(tenantId, phone);
                    return result > 0;
                default:
                    return false;
            }
        } catch (Exception e) {
            return false;
        }

    }
}
